package timlcy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;

import timlcy.po.Message;
import timlcy.util.CheckUtil;
import timlcy.util.MessageUtil;
import timlcy.util.WeixinUtil;

@WebServlet(urlPatterns = { "/wx" })
public class Timlcy extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		PrintWriter out = response.getWriter();
		if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
			out.print(echostr);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		try (PrintWriter out = response.getWriter()) {
			Map<String, String> map = MessageUtil.xmlToMap(request);
			String ToUserName = map.get("ToUserName");
			String FromUserName = map.get("FromUserName");
			String Content = map.get("Content");
			String MsgType = map.get("MsgType");
			String ms = null;
			if ((MessageUtil.MESSAGE_TEXT).equals(MsgType)) {
				if ("1".equals(Content)) {
					ms = MessageUtil.initText(ToUserName, FromUserName, MessageUtil.firstMenu());
				} else if ("2".equals(Content)) {
					ms = MessageUtil.initNewsMessage(ToUserName, FromUserName);

				} else if ("3".equals(Content)) {
					ms = MessageUtil.initImageMessage(ToUserName, FromUserName);
				} else if ("4".equals(Content)) {
					ms = MessageUtil.initMusicMessage(ToUserName, FromUserName);
				} else if ("5".equals(Content)) {
					ms = MessageUtil.initText(ToUserName, FromUserName, MessageUtil.threeMenu());
				} else if (Content.startsWith("翻译")) {
					String word = Content.replaceAll("^翻译", "").trim();
					if ("".equals(word)) {
						ms = MessageUtil.initText(ToUserName, FromUserName, MessageUtil.threeMenu());
					} else {
						ms = MessageUtil.initText(ToUserName, FromUserName, WeixinUtil.translate(word));
					}
				}

				else if ("?".equals(Content) || "？".equals(Content)) {
					ms = MessageUtil.initText(ToUserName, FromUserName, MessageUtil.menuText());
				}

			} else if ((MessageUtil.MESSAGE_EVENT).equals(MsgType)) {
				String evenType = map.get("Event");
				if ((MessageUtil.MESSAGE_SUBSCRIBE).equals(evenType)) {
					ms = MessageUtil.initText(ToUserName, FromUserName, MessageUtil.menuText());
				} else if ((MessageUtil.MESSAGE_CLICK).equals(evenType)) {
					ms = MessageUtil.initText(ToUserName, FromUserName, MessageUtil.menuText());
				} else if ((MessageUtil.MESSAGE_VIEW).equals(evenType)) {
					String url = map.get("EventKey");
					ms = MessageUtil.initText(ToUserName, FromUserName, url);
				} else if ((MessageUtil.MESSAGE_SCANCODE).equals(evenType)) {
					String key = map.get("EventKey");
					ms = MessageUtil.initText(ToUserName, FromUserName, key);
				}
			} else if ((MessageUtil.MESSAGE_LOCATION).equals(MsgType)) {
				String label = map.get("Label");
				ms = MessageUtil.initText(ToUserName, FromUserName, label);
			}

			System.out.println(ms);
			out.print(ms);

		} catch (DocumentException e) {

			e.printStackTrace();
		}
	}

}
